package com.wzdigit.wms.wms.controller.stockIn;

import com.wzdigit.framework.Shift;
import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.domain.dtos.inventory.SnInforDto;
import com.wzdigit.wms.basic.enums.AsnTypeEnum;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.basic.client.request.asn.SnListRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.receiveGoods.PalletizerResultDto;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.order.ReceiveGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


@RestController
@Api(tags = "下线交收")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class ReceiveGoodsController {

    /*日志工具*/
    private final Logger logger = LoggerFactory.getLogger(Object.class);

    private final String REQUEST_MODEL = "/ReceiveGoods";

    @Autowired
    private ReceiveGoodsService service;


    @ApiOperation(value = "[码垛机]多sn收货", notes = "码垛机收货")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = "/external/palletizerReceive", method = RequestMethod.POST)

    public PalletizerResultDto palletizerSnReceive(@Valid @RequestBody SnListRequest input) throws Exception {
        PalletizerResultDto dto = new PalletizerResultDto();
        try {
            if (input.getPRODUCT_SN().isEmpty())
                Shift.fatal("sn信息不能为空");
            if (input.getPRODUCT_SN().size() == 0)
                Shift.fatal("sn信息不能为空");
            String userName = String.valueOf(input.getID());
            Result result = this.service.receive(input.getPRODUCT_SN(), userName, "码垛机接收", AsnTypeEnum.R01.code);

            dto.setID(input.getID());
            dto.setSTATUS(result.isFlag() ? "T" : "F");
            dto.setMSG(result.getMessage());
            return dto;
        } catch (RestStatusException ex) {
            dto.setID(input.getID());
            dto.setSTATUS("F");
            dto.setMSG(ex.getContent());
        }
        return dto;
    }

    @ApiOperation(value = "多sn收货(人工收货)", notes = "多sn收货(人工收货)")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/artificialReceive", method = RequestMethod.POST)
    public Result artificialReceive(@Valid @RequestBody SnListRequest input,
                                    HttpServletRequest servletRequest,
                                    HttpServletResponse servletResponse) throws Exception {
        try {
            if (input.getPRODUCT_SN().isEmpty())
                Shift.fatal("sn信息不能为空");
            if (input.getPRODUCT_SN().size() == 0)
                Shift.fatal("sn信息不能为空");

            String userName = HttpSessionUtil.getLoginName(servletRequest);
            return this.service.receive(input.getPRODUCT_SN(), userName, "人工接收", AsnTypeEnum.R96.code);
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    @ApiOperation(value = "扫描sn", notes = "扫描sn")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/scan", method = RequestMethod.GET)
    public Result<List<SnInforDto>> palletizerSnReceive(String sn,
                                                        HttpServletRequest servletRequest,
                                                        HttpServletResponse servletResponse) {
        try {
            return this.service.scan(sn,servletRequest);
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    @ApiOperation(value = "[码垛机]扫描sn", notes = "[码垛机]扫描sn")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = "/external/scan", method = RequestMethod.GET)
    public Result<List<SnInforDto>> palletizerSnScan(String sn,
                                                     HttpServletRequest servletRequest,
                                                     HttpServletResponse servletResponse) {
        Result<List<SnInforDto>> dto = new Result<List<SnInforDto>>();
        try {
            return this.service.scan(sn,servletRequest);
        } catch (RestStatusException ex) {
            //servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
//            throw ex;
            dto.setFlag(false);
            dto.setMessage(ex.getContent());
            dto.setData(null);
        }
        return dto;
    }


    /**
     * 获取缓存
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "获取缓存", notes = "获取缓存")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/getCache", method = RequestMethod.GET)
    public Result<List<SnInforDto>> getCache(HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return service.getCache(servletRequest,servletResponse);
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }


    /**
     * 获取缓存
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "删除缓存", notes = "删除缓存")
    @DeleteMapping(value = REQUEST_MODEL + "/deleteCache")
    public Result deleteCache(HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return service.deleteCache(servletRequest,servletResponse);
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

}
